import 'package:flutter/material.dart';
import 'package:flutterdemo/utils/toast_util.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UrlLauncherPageState();
  }
}

class _UrlLauncherPageState extends State<UrlLauncherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('url_launcher'),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: _body(),
      ),
    );
  }

  Widget _body() {
    return Column(
      children: <Widget>[
        RaisedButton.icon(
          onPressed: () async {
            String url = 'tel:18039670989';
            if (await canLaunch(url)) {
              launch(url);
            } else {
              ToastUtil.showToastCenter('could not launch [$url]');
            }
          },
          icon: Icon(Icons.phone),
          label: Text('拨打电话'),
        ),
        RaisedButton.icon(
          onPressed: () async {
            String url = 'https://baidu.com';
            if (await canLaunch(url)) {
              launch(url);
            } else {
              ToastUtil.showToastCenter('could not launch [$url]');
            }
          },
          icon: Icon(Icons.language),
          label: Text('打开网页'),
        ),
        RaisedButton.icon(
          onPressed: () async {
            launch(Uri(
              scheme: 'mailto',
              path: '872235631@qq.com',
              queryParameters: {"param": "参数"},
            ).toString());
          },
          icon: Icon(Icons.email),
          label: Text('发送邮件'),
        ),
      ],
    );
  }
}
